﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetworkManager;
using Shakes_and_Fidget___Charplaner.Classes;

namespace Shakes_and_Fidget___Charplaner
{
    public partial class AttributeBox : Form
    {
		public NetworkManager.NetworkManager nm = new NetworkManager.NetworkManager();
        public AttributeBox()
        {
			this.Hide();
            InitializeComponent();
			this.Size = new Size(283, 277);
        }
		public string characterinfo;
		public bool success = false;
		private void expand_Click(object sender, EventArgs e)
		{
			this.Size = new Size(647, 277);
			expand.Visible = false;
			contract.Visible = true;
			loadcharbox.Visible = true;
			this.CenterToScreen();
		}
		private void contract_Click(object sender, EventArgs e)
		{
			this.Size = new Size(283, 277);
			expand.Visible = true;
			contract.Visible = false;
			loadcharbox.Visible = false;
			this.CenterToScreen();
		}
		private void load_Click(object sender, EventArgs e)
		{
			processbar.Value = 0;
			accid.Text = "n/A";
			loadingtime.Text = "Aktualisiert:";
			description.Text = "Charakterbeschreibung:\r\n";
			//process.Text = "Trying to get Cache Path";
			//characterinfo = GetBrowserCachePath(browser.SelectedIndex);
			nm.SetAccountInfo(pass.Text, accname.Text);
			//nm.GetSessionID();
			characterinfo = nm.DoRequest("004");
			success = true;
		}
		string GetBrowserCachePath(int browserid)
		{
			switch (browserid)
			{
				case 0: //Firefox
					processbar.Value += 5;
					process.Text = "Looking up Firefox Cache Path";
					DirectoryInfo profilefolder = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Mozilla\\Firefox\\Profiles\\");
					DirectoryInfo[] profiles = profilefolder.GetDirectories();
					process.Text = "Getting Firefox Cache";
					processbar.Value += 5;
					foreach (DirectoryInfo dir in profiles)
					{
						processbar.Value += 10 / profiles.Length;
						process.Text += ".";
						FileStream cache = new FileStream(dir.FullName + "\\Cache\\_CACHE_001_", FileMode.Open, FileAccess.Read, System.IO.FileShare.ReadWrite);	
					}
					process.Text = "Got Firefox Cache Path";
					processbar.Value += 5;
					return "";
					//break;
				case 1: // IE
					process.Text = "Looking up IE Cache Path";
					processbar.Value += 5;
					string cache_path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\Content.IE5\";
					DirectoryInfo cachefolder = new DirectoryInfo(cache_path);
					DirectoryInfo[] userinfo = cachefolder.GetDirectories();
					processbar.Value += 5;
					process.Text = "Getting IE Cache";
					foreach (DirectoryInfo f in userinfo)
					{
						processbar.Value += 10 / userinfo.Length;
						process.Text = "Searching relevant files";
						FileInfo[] b = f.GetFiles("*req*.htm");
						foreach (FileInfo fi in b)
						{
							process.Text += ".";
							processbar.Value += 10 / b.Length;
							StreamReader content = fi.OpenText();
							string cont = content.ReadToEnd().ToString();
							if (cont[cont.Length - 1] == ';')
							{
								success = true;
								processbar.Value += 5;
								process.Text = "Got character information file!";
								return cont;
							}
						}
						
					}
					return "";
					//break;
				case 2: //Opera
					return "";
					//break;
				case 3: //Chrome
					return "";
					//break;
				default:
					return "";
					//break;
			}
		}

		private void something_TextChanged(object sender, EventArgs e)
		{
			if (this.accname.Text != "" && this.pass.Text != "" && this.server.Text != "")
				this.load.Enabled = true;
			else
				this.load.Enabled = false;
		}
    }
}
